<script setup lang="ts">
import { ref } from "vue";

import { useRouter } from "vue-router";
// 消息弹框
import { ElMessage, ElNotification } from "element-plus";
// 引入仓库，token
import useUserStore from "@/stores/modules/user";
import { getTime } from '@/utils/time'

//获取el-form组件
let loginForms = ref()
// 获取路由器
let $router = useRouter();
//登录按钮加载中
let loading = ref(false);

// 自定义校验规则需要的函数
const validateUserName = ( rule: any , value: any, callback: any)=> {
  // console.log(value);
  
  if(value.trim().length>=5) {
    callback()
  } else{
    callback(new Error('账号长度至少5位'))
  }
}
// 表单校验规则
const rules = {
  //对应的prop需要和userName一样不能username
  userName: [
  // { required: true, message: '用户名不能为空', trigger: 'blur'  },
  { trigger: 'change' , validator: validateUserName}

  ],
  passWord: []
}

//登录数据
let loginForm = ref({ userName: "adminss", passWord: "admin888" });
//找回密码数据
let findPassword= ref({email: '2506862882@qq.com',passWord: 'adminss888',passWord_confirm: 'adminss888'})


// 验证邮箱验证码
let validateEmail = ref({
  key: '',
  value: ''
})
let emailSender= ref({email:'2506862882@qq.com'})

let userStore = useUserStore();
const shift = ref(0);//0是登录，1是注册，2是找回密码

//登录逻辑
const login = async () => {
  // console.log(loginForms.value);
  // 表单校验成功了再发送请求
  await loginForms.value.validate()  //返回一个promise对象
  
  loading.value = true;
  try {
    // 保证登陆成功，发送请求,携带参数
    await userStore.userLogin(loginForm.value);
    //  编程式导航跳转到首页
    $router.push("/home/get");
    ElNotification({
      type: "success",
      title:'Hi!   '+  getTime()+ '好',
      message: '欢迎回来'
    });
    loading.value = false;
  } catch (error) {
    loading.value = false;
    ElNotification({
      type: "error",
      message: (error as Error).message 
    });
  }
};


//获取验证码
const sendCaptcha= async () => {
try {
  await userStore.getCaptcha(emailSender.value).then(key => {
  validateEmail.value.key =key
})
} catch (error) {
  console.log(error);
  
  ElNotification({
      type: "error",
      message: '发送验证码失败'
    });
}
}
// 验证邮箱验证码
const validateEmailCodeHandle =async ()=> {
  try {
  await  userStore.validateEmailCode(validateEmail.value )
  } catch (error) {
    ElMessage.error('验证码输入错误')
  }
}
//找回密码
const findPasswordHandle = async ()=> {
try {
  validateEmailCodeHandle()
  await userStore.findePassword(findPassword.value)
} catch (error) {
  ElNotification({
      type: "error",
      message: (error as Error).message 
    });
  
}
}

</script>

<template>
  <link
    rel="stylesheet"
    href="//at.alicdn.com/t/c/font_4195471_e3acor9ry89.css"
  />

  <el-row class="login-page">
    <el-col :span="12" class="bg"> </el-col>
    <el-col :span="6" :offset="3" class="form">
      <div class="shiftButton">
        <!-- 展示注册 -->
        <el-button @click="shift = 0 " v-if="shift === 1">
          登录
        </el-button>
        <!-- 展示登录 -->
        <el-button @click="shift = 1" v-else> 注册 </el-button>
      </div>
<!-- 注册 -->
      <el-form
        ref="form"
        size="large"
        autocomplete="off"
        v-if="shift === 1"
        class="login-form"
      >
        <div class="form-items">
          <el-form-item>
            <h1>注册</h1>
          </el-form-item>
          <el-form-item>
            <div class="input_box">
              <el-input placeholder="请输入用户名" class="radius">
                <template #prefix>
                  <el-icon class="el-input__icon">
                    <span class="iconfont"> &#xe62f; </span></el-icon
                  >
                </template>
              </el-input>
            </div>
          </el-form-item>
          <el-form-item>
            <div class="input_box">
              <el-input placeholder="请输入邮箱">
                <template #prefix>
                  <el-icon class="el-input__icon">
                    <span class="iconfont">&#xe908;</span></el-icon
                  >
                </template>
              </el-input>
            </div>
          </el-form-item>

          <el-form-item>
            <div class="input_box">
              <el-input type="password" placeholder="请输入密码">
                <template #prefix>
                  <el-icon class="el-input__icon">
                    <span class="iconfont">&#xe698;</span></el-icon
                  >
                </template>

                <template #suffix>
                  <el-icon class="el-input__icon">
                    <span class="iconfont">&#xe675;</span></el-icon
                  >
                </template>
              </el-input>
            </div>
          </el-form-item>
          <el-form-item>
            <div class="input_box">
              <el-input type="password" placeholder="请输入再次密码">
                <template #prefix>
                  <el-icon class="el-input__icon">
                    <span class="iconfont">&#xe698;</span></el-icon
                  >
                </template>
                <!-- 使用插槽加入图标 -->
                <template #suffix>
                  <el-icon class="el-input__icon">
                    <span class="iconfont">&#xe675;</span></el-icon
                  >
                </template>
              </el-input>
            </div>
          </el-form-item>

          <el-form-item class="captcha">
            <!-- 验证码 -->
            <div class="input_captcha">
              <el-input placeholder="验证码">
                <template #prefix>
                  <el-icon class="el-input__icon">
                    <span class="iconfont">&#xe624;</span></el-icon
                  >
                </template>
              </el-input>
            </div>

            <div class="btn_captcha">
              <el-button> 点击获取验证码 </el-button>
            </div>
          </el-form-item>

          <el-form-item>
            <div class="btn_common">
              <el-button class="button" type="primary" auto-insert-space>
                注册
              </el-button>
            </div>
          </el-form-item>
        </div>
      </el-form>


      <!-- 登录 -->
      <el-form
      :model = "loginForm"
      :rules = "rules"
        ref="loginForms"
        size="large"
        autocomplete="off"
        class="login-form"
        v-if="shift === 0"
      >
        <div class="form-items">
          <el-form-item>
            <h1>登录</h1>
          </el-form-item>
          <el-form-item prop="userName">
            <div class="input_box">
              <el-input  placeholder="请输入用户名" v-model="loginForm.userName">
                <template #prefix>
                  <el-icon class="el-input__icon">
                    <span class="iconfont"> &#xe62f; </span></el-icon
                  >
                </template>
              </el-input>
            </div>
          </el-form-item>
          <el-form-item prop="passWord">
            <div class="input_box">
              <el-input
                type="password"
                placeholder="请输入密码"
                v-model="loginForm.passWord"
                show-password
              >
                <template #prefix>
                  <el-icon class="el-input__icon">
                    <span class="iconfont">&#xe698;</span></el-icon
                  >
                </template>
              </el-input>
            </div>
          </el-form-item>
          <el-form-item >
            <div class="remeberme">
              <el-checkbox>记住我</el-checkbox>
              <el-link type="primary" :underline="false" @click="shift = 2">忘记密码？</el-link>
            </div>
          </el-form-item>
          <el-form-item>
            <div class="btn_common">
              <el-button
                :loading="loading"
                class="button"
                type="primary"
                auto-insert-space
                @click="login"
                >登录</el-button
              >
            </div>
          </el-form-item>
        </div>
      </el-form>
      <!-- 找回密码 -->
      <el-form
        ref="form"
        size="large"
        autocomplete="off"
        class="login-form"
        v-if="shift === 2"
      >
        <div class="form-items">
          <el-form-item>
            <h1>找回密码</h1>
          </el-form-item>
         
          <el-form-item prop="email"> 
            <div class="input_box">
              <el-input placeholder="请输入您的邮箱" v-model="findPassword.email">
                <template #prefix>
                  <el-icon class="el-input__icon">
                    <span class="iconfont">&#xe908;</span></el-icon
                  >
                </template>
              </el-input>
            </div>
          </el-form-item>

          <el-form-item prop="passWord">
            <div class="input_box">
              <el-input type="password" placeholder="请输入您的密码" v-model="findPassword.passWord">
                <template #prefix>
                  <el-icon class="el-input__icon">
                    <span class="iconfont">&#xe698;</span></el-icon
                  >
                </template>

                <template #suffix>
                  <el-icon class="el-input__icon">
                    <span class="iconfont">&#xe675;</span></el-icon
                  >
                </template>
              </el-input>
            </div>
          </el-form-item>
          <el-form-item prop="passWord_confirm"> 
            <div class="input_box">
              <el-input type="password" placeholder="请确认您的密码" v-model="findPassword.passWord_confirm">
                <template #prefix>
                  <el-icon class="el-input__icon">
                    <span class="iconfont">&#xe698;</span></el-icon
                  >
                </template>
                <!-- 使用插槽加入图标 -->
                <template #suffix>
                  <el-icon class="el-input__icon">
                    <span class="iconfont">&#xe675;</span></el-icon
                  >
                </template>
              </el-input>
            </div>
          </el-form-item>

          <el-form-item class="captcha">
            <!-- 验证码 -->
            <div class="input_captcha">
              <el-input placeholder="验证码" v-model="validateEmail.value">
                <template #prefix>
                  <el-icon class="el-input__icon">
                    <span class="iconfont">&#xe624;</span></el-icon
                  >
                </template>
              </el-input>
            </div>

            <div class="btn_captcha">
              <el-button @click="sendCaptcha"> 点击获取验证码 </el-button>
            </div>
          </el-form-item>

          <el-form-item>
            <div class="btn_common">
              <el-button class="button" type="primary" auto-insert-space @click="findPasswordHandle">
                点击找回
              </el-button>
            </div>
          </el-form-item>
        </div>
      </el-form>
    </el-col>
  </el-row>
</template>

<style lang="scss" scoped>
h1{
  margin-top: 10px;
}

.login-page {
  height: 100vh;
  background-color: #fff;
  background: url("@/assets/images/background.png") no-repeat center / cover; //logo
  .bg {
    background: url("@/assets/images/logo左侧组(1).png") no-repeat 60% center /
      180px //背景图片
      auto;
  }
  .shiftButton {
    border-radius: 95px;
    // background: rgba(225, 226, 229, 0.28);
    position: absolute;
    top: 28px;
    right: 20px;
    width: 100px;
    height: 50px;
    background: rgb(255, 255, 255, 0.4);
    text-align: center;
    line-height: 50px;
    font-size: 20px;
    color: #322e2e;
    // border-radius: 25px;
    border: none;
    outline: none;
  }
  .login-form {
    border: 1px solid #a1a3ab;
    border-radius: 30px;
    background: rgb(255, 255, 255, 0.25);
    .form-items {
      position: relative;
      left: 30px;
    }
  }
  // 输入框改成椭圆，背景改成透明
  .input_box {
    width: 80%;
    margin-right: 15px;
    border-radius: 95px;
    background: rgba(225, 226, 229, 0.28);
    // color:#64616b
  }

  // 验证码input框
  .input_captcha {
    width: 40%;
    margin-right: 15px;
    border-radius: 95px;
    background: rgba(225, 226, 229, 0.28);
    color: #64616b;
    flex: 1; //同行占比
    margin-right: 10px;
  }
  //验证码对齐
  .captcha {
    display: flex;
    align-items: center;
    width: 80%;
  }

  /*input框 */
  :deep(.el-input__wrapper) {
    background-color: transparent; /*覆盖原背景颜色，设置成透明 */
    border-radius: 95px;
    border: 0;
    box-shadow: 0 0 0 0px;
    // color:#64616b;
  }
  :deep(.el-input__inner::placeholder) {
    color: #64616b;
  }
  :deep(.el-link__inner) {
    color: #64616b;
  }

  :deep(.el-input__icon) {
    color: #64616b; /* 修改为您想要的颜色 */
  }

  //button
  .btn_captcha {
    flex: 0;
    width: 60%;
    border-radius: 95px;
    background: rgba(225, 226, 229, 0.28);
  }
  .btn_common {
    width: 80%; //所有按钮宽度
    border-radius: 95px;
    background: rgba(225, 226, 229, 0.28);
  }
  :deep(.el-button) {
    border-radius: 95px;
    background-color: transparent; /*覆盖原背景颜色，设置成透明 */
    border: 0;
    box-shadow: 0 0 0 0px;
  }

  h1 {
    color: #fff;
  }
  .form {
    // border: 1px solid #f00;
    display: flex;
    flex-direction: column;
    justify-content: center;
    user-select: none;
    .title {
      margin: 0 auto;
    }
    .button {
      width: 100%;
    }
    .remeberme {
      width: 80%;
      display: flex;
      justify-content: space-between;
    }
  }
}
</style>
